package com.ruoyi.web.controller.hz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HzDanmaku;
import com.ruoyi.system.service.IHzDanmakuService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 弹幕实体Controller
 *
 * @author ruoyi
 * @date 2021-08-08
 */
@Controller
@RequestMapping("/danmaku")
public class HzDanmakuController extends BaseController {
    private String prefix = "danmaku";

    @Autowired
    private IHzDanmakuService hzDanmakuService;

    @GetMapping()
    public String danmaku() {
        return prefix + "/danmaku";
    }

    @GetMapping("/websocketTest")
    public String websocketTest(ModelMap mmap) {
        System.out.println("webSocKetC测试!");
        return "websocket";
    }

    /**
     * 新增弹幕
     * String author,String color,String id,String text,String time,String right
     */
    @PostMapping("/danmakuIng")
    @ResponseBody
    public AjaxResult danmakuAdd(@RequestBody Map<String, String> map) {
        SysUser user = ShiroUtils.getSysUser();
        String uploadInfoId = map.get("id");
        String text = map.get("text");
        String msgSeek = map.get("time");
        HzDanmaku hzDanmaku = new HzDanmaku(uploadInfoId, user.getUserId(), text, msgSeek);
        hzDanmakuService.insertHzDanmaku(hzDanmaku);
        return AjaxResult.success();
    }


    /**
     * 弹幕加载
     *
     * @param id  视频id
     * @param max 最大加载条数
     * @return
     */
    @GetMapping("/danmakuIng")
    @ResponseBody
    public AjaxResult danmakuLoad(String id, String max) {
        List tolist = new ArrayList();
        if (id == null || id.isEmpty()) {
            String[] msg = {1 + "", "0", "16777215", "618c713c", "弹幕大军来了来了~~~~~~~"};
            tolist.add(msg);
            return AjaxResult.success(tolist);
        }
        HzDanmaku hzDanmaku = new HzDanmaku();
        hzDanmaku.setUploadInfoId(Long.parseLong(id));
        List<HzDanmaku> list = hzDanmakuService.selectHzDanmakuList(hzDanmaku);

        for (HzDanmaku dk : list) {
            String[] msg = {dk.getMsgSeek(), "0", "16777215", "618c713c", dk.getSengMsg()};
            tolist.add(msg);
        }
        return AjaxResult.success(tolist);
    }


    /**
     * 查询弹幕实体列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HzDanmaku hzDanmaku) {
        startPage();
        List<HzDanmaku> list = hzDanmakuService.selectHzDanmakuList(hzDanmaku);
        return getDataTable(list);
    }

    /**
     * 导出弹幕实体列表
     */
    @RequiresPermissions("system:danmaku:export")
    @Log(title = "弹幕实体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HzDanmaku hzDanmaku) {
        List<HzDanmaku> list = hzDanmakuService.selectHzDanmakuList(hzDanmaku);
        ExcelUtil<HzDanmaku> util = new ExcelUtil<HzDanmaku>(HzDanmaku.class);
        return util.exportExcel(list, "弹幕实体数据");
    }

    /**
     * 新增弹幕实体
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存弹幕实体
     */
    @RequiresPermissions("system:danmaku:add")
    @Log(title = "弹幕实体", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HzDanmaku hzDanmaku) {
        return toAjax(hzDanmakuService.insertHzDanmaku(hzDanmaku));
    }

    /**
     * 修改弹幕实体
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        HzDanmaku hzDanmaku = hzDanmakuService.selectHzDanmakuById(id);
        mmap.put("hzDanmaku", hzDanmaku);
        return prefix + "/edit";
    }

    /**
     * 修改保存弹幕实体
     */
    @RequiresPermissions("system:danmaku:edit")
    @Log(title = "弹幕实体", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HzDanmaku hzDanmaku) {
        return toAjax(hzDanmakuService.updateHzDanmaku(hzDanmaku));
    }

    /**
     * 删除弹幕实体
     */
    @RequiresPermissions("system:danmaku:remove")
    @Log(title = "弹幕实体", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hzDanmakuService.deleteHzDanmakuByIds(ids));
    }
}
